package Engine;

import java.util.ArrayList;
import java.util.Random;

import ScarvangerHuntEngine.SHMission;
import ScarvangerHuntEngine.SHQuest;

public class Quest {
	private Mission lastMission;
	protected ArrayList<Mission> missions;
	protected Mission firstMission;
	public Quest()
	{
		missions = new ArrayList<Mission>();
	}
	public Quest(ArrayList<Mission> missions)
	{
		this.setMissions(missions);
	}
	
	public Mission GetRandomMission()
	{
		ArrayList<Mission> notAccomplished = GetMissions(false);
		if(notAccomplished.size() != 0)
		{
			Random rdm = new Random();
			int nextIndex = rdm.nextInt(notAccomplished.size());
			return notAccomplished.get(nextIndex);
		}
		else
			return firstMission;
	}
	private ArrayList<Mission> GetMissions(boolean onlyAccomplished)
	{
		ArrayList<Mission> accomplished = new ArrayList<Mission>();
		Mission previous = lastMission;
		while(previous != this.firstMission)
		{
			accomplished.add(previous);
			previous = previous.getPreviousMission();
		}
		
		if(onlyAccomplished)
			return accomplished;
		else
		{
			ArrayList<Mission> result = new ArrayList<Mission>(missions);
			for (Mission mission : accomplished) {
				result.remove(mission);
			}
			result.remove(firstMission);
			return result;
		}
		
//		ArrayList<Mission> accomplished = new ArrayList<Mission>();
//		accomplished.add(missions.get(0));
//		if(onlyAccomplished)
//			return GetMissionsFromPrevious(missions.get(0), accomplished);
//		else
//		{
//			ArrayList<Mission> notAccomplished = new ArrayList<Mission>(missions);
//			notAccomplished.removeAll(accomplished);
//			return notAccomplished;
//		}
	}
//	private ArrayList<Mission> GetMissionsFromPrevious(Mission previous, ArrayList<Mission> AccomplishedMissions)
//	{
//		for (Mission next : missions) {
//			if(next.getPreviousMission() == previous)
//			{
//				GetMissionsFromPrevious(next.getPreviousMission(), AccomplishedMissions);
//				AccomplishedMissions.add(next);
//			}
//		}
//		return AccomplishedMissions;
//	}

	public Mission CalculateLastMission()
	{
		for (Mission m : missions) {
			if(m.getPreviousMission() != null)
			{
				boolean isLast = true;
				for (Mission m2 : missions) {
					if(m2.getPreviousMission() == m)
						isLast = false;
				}
				if(isLast)
					this.lastMission = m;
			}
		}
		return null;
	}
	
	public boolean[] getMissionStatus()
	{
		//initialising result
		boolean[] result = new boolean[missions.size()];
		
		Mission previous = lastMission;
		while(previous != this.firstMission)
		{
			previous = previous.getPreviousMission();
			result[previous.getId()] = true;
		}
//		//for each missions
//		for (int i=0;i< missions.size();i++) 
//		{
//			//Initialising the result of the mission
//			result[i] = false;
//			if(missions.get(i).getPreviousMission() != null)
//			{
//				for (Mission m : missions) {
//					if(m.getPreviousMission() == missions.get(i))
//					{
//						//we replace result by true if the mission got a next one
//						result[i] = true;
//						break;
//					}
//				}
//			}
//		}
		return result;
	}
	
	
	
	public void setLastMission(Mission lastMission) {
		this.lastMission = lastMission;
	}
	public Mission getLastMission() {
		return lastMission;
	}
	public void setMissions(ArrayList<Mission> missions) {
		this.missions = missions;
	}
	public ArrayList<Mission> getMissions() {
		return missions;
	}
}
